import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:tiktokadult/hj_page/live/live_service.dart';
import 'package:tiktokadult/hj_page/mine_business/mine_vip/mine_charge_vip_page.dart';

import 'live_main_logic.dart';
import 'live_model.dart';

//直播亚模块
class LiveSubLogic extends GetxController {
  LiveModule? tagData;
  int currentPage = 1;
  bool loading = true;
  List<LiveAnchor> liveList = []; //
  RefreshController? controller;
  bool get isRecom => tagData?.id == '-1'; //是否是推荐模块
  LiveMainLogic get mainLogic => Get.find<LiveMainLogic>();
  LiveMainModel? liveMainModel;

  LiveSubLogic({this.tagData});

  @override
  onReady() {
    super.onReady();
    //默认数据
    liveMainModel = mainLogic.liveMainModel;
    loadData();
  }

  //获取专题数据
  loadData({int pageNum = 1, bool showLoading = false}) async {
    if (isRecom) {
      loadRecommendData();
      update();
    } else {
      loadCountryAnchor(pageNum: pageNum);
    }
  }

  //推荐直播列表
  loadRecommendData() async {
    final result = await LiveService.getLiveModuleList();
    if (result != null) {
      liveMainModel = result;
    }
    controller?.refreshCompleted();
    update();
  }

  //tag直播列表
  loadCountryAnchor({int pageNum = 1, bool showLoading = false}) async {
    LiveAnchors? liveMainModels = await LiveService.getLiveAnchorList(
      id: tagData?.id ?? '',
      pageNumber: pageNum,
    );
    loading = false;
    if (liveMainModels != null) {
      currentPage = pageNum;
      if (currentPage == 1) liveList.clear();
      if (liveMainModels.list != null && liveMainModels.list?.isNotEmpty == true) {
        liveList.addAll(liveMainModels.list ?? []);
      }
    }
    update();
    liveMainModels?.hasNext == true ? controller?.loadComplete() : controller?.loadNoData();
    controller?.refreshCompleted();
  }

  loadMoreData() => loadData(pageNum: currentPage + 1);

  //跳转充值
  onChargeAction() {
    Get.to(MineChargeVipPage(), preventDuplicates: false);
  }
}
